package window.panel;

import util.CityCache;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Set;

public class FilterSortPanel extends JPanel {

    private JComboBox<String> cityComboBox;  // 将城市选择框声明为成员变量
    private JComboBox<String> priceComboBox; // 将价格排序选择框声明为成员变量
    private JComboBox<String> starComboBox;  // 将星级排序选择框声明为成员变量

    public FilterSortPanel() {
        setLayout(new FlowLayout(FlowLayout.CENTER, 30, 0)); // 设置为左对齐的流式布局

        // City Filter
        JPanel cityFilterPanel = new JPanel();
        JLabel cityLabel = new JLabel("City Filter:");
        Set<String> citySet = CityCache.getCitiesOfHotels(); // 从缓存中获取城市列表
        ArrayList<String> citiesArray = new ArrayList<>();
        citiesArray.add("All Cities");
        citiesArray.addAll(citySet);
        String[] cities = citiesArray.toArray(new String[0]);
        cityComboBox = new JComboBox<>(cities);  // 将 JComboBox 赋值给成员变量
        cityComboBox.setSelectedItem("Beijing");
        cityFilterPanel.add(cityLabel);
        cityFilterPanel.add(cityComboBox);

        // Price Sort
        JPanel priceSortPanel = new JPanel();
        JLabel priceSortLabel = new JLabel("Sort by Price:");
        String[] priceOptions = {"Low to High", "High to Low"};
        priceComboBox = new JComboBox<>(priceOptions);  // 将 JComboBox 赋值给成员变量
        priceSortPanel.add(priceSortLabel);
        priceSortPanel.add(priceComboBox);

        // Star Rating Sort
        JPanel starSortPanel = new JPanel();
        JLabel starSortLabel = new JLabel("Sort by Star Rating:");
        String[] starOptions = {"Low to High", "High to Low"};
        starComboBox = new JComboBox<>(starOptions);  // 将 JComboBox 赋值给成员变量
        starSortPanel.add(starSortLabel);
        starSortPanel.add(starComboBox);

        // Adding components to the panel
        add(cityFilterPanel);
        add(priceSortPanel);
        add(starSortPanel);
    }

    // 提供一个方法来获取选中的城市
    public String getSelectedCity() {
        return (String) cityComboBox.getSelectedItem();
    }

    // 提供一个方法来获取选中的价格排序
    public String getSelectedPriceSort() {
        return (String) priceComboBox.getSelectedItem();
    }

    // 提供一个方法来获取选中的星级排序
    public String getSelectedStarSort() {
        return (String) starComboBox.getSelectedItem();
    }

    public JComboBox<String> getCityComboBox() {
        return cityComboBox;
    }

    public JComboBox<String> getPriceComboBox() {
        return priceComboBox;
    }

    public JComboBox<String> getStarComboBox() {
        return starComboBox;
    }

    public static void main(String[] args) {
        // Create the frame
        JFrame frame = new JFrame("Filter and Sort Panel");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(800, 80);
        frame.setLocationRelativeTo(null); // Center the frame

        // Add the filter and sort panel
        FilterSortPanel filterSortPanel = new FilterSortPanel();
        frame.add(filterSortPanel);

        // Display the frame
        frame.setVisible(true);
    }
}
